/**
 * 
 */
package com.acxiom.bsh.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acxiom.bsh.constants.CommonConstant;
import com.acxiom.bsh.entity.DrawResult;
import com.acxiom.bsh.exception.BshException;
import com.acxiom.bsh.pojo.PrizeRecord;
import com.acxiom.bsh.service.LuckydrawService;

/**
 * @author luciyu
 *
 */
@Controller
public class LuckyDrawController {

	@Autowired
	private LuckydrawService luckydrawService;
	
	/**
	 * 抽奖
	* @Title: draw  抽奖功能
	* @param session  存储淘宝用户信息会话
	* @return
	 */
	@RequestMapping(value = "/draw")
	@ResponseBody
	public DrawResult draw(HttpSession session){
		String mixNick = getNickName(session);
		if (mixNick == null) {
			return null;
		}
		DrawResult result = null;
		try {
			result = luckydrawService.draw(mixNick);
			return result;
		} catch (Exception e) {
			if(result == null) {
				result = new DrawResult();
			}
			if(StringUtils.isEmpty(result.getCode())){
				result.setCode("099");
				result.setMessage("系统正忙，请稍后");
			}
		}
		return result;
	}
	/**
	 * 抽奖记录
	* @Title: drawList  抽奖记录
	* @param session  存储淘宝用户信息会话
	* @return
	 */
	@RequestMapping(value = "/draw/list")
	@ResponseBody
	public List<PrizeRecord> drawList(HttpSession session) throws BshException{
		String mixNick = getNickName(session);
		if (mixNick == null) {
			return null;
		}
		List<PrizeRecord> result = luckydrawService.getPrizeList(mixNick);
		return result;
	}
	
	/**
	 * 抽奖top5名单
	* @Title: getPrizeRecordTop5  抽奖top5名单
	* @param session  存储淘宝用户信息会话
	* @return
	 */
	@RequestMapping(value = "/draw/top5")
	@ResponseBody
	public List<PrizeRecord> getPrizeRecordTop5(HttpSession session) throws BshException {
		List<PrizeRecord> result = null;
		try {
			String mixNick = getNickName(session);
			if (mixNick == null) {
				return null;
			}
			result = luckydrawService.getPrizeListTop5();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取用户nick信息
	* @Title: getNickName
	* @param session 存储淘宝用户信息会话
	* @return
	 */
	private String getNickName(HttpSession session) {
		Object obj = session.getAttribute(CommonConstant.TAOBAO_NICK_ATTRIBUTE);
		String nickName = null;
		if (obj != null) {
			nickName = obj.toString();
		} 
		return nickName;
	}
}
